/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:17:32
 * @Last Modified by: songwh
 * @Last Modified time: 2018-03-29 14:55:15
*/

import { modal } from 'vue-strap'
import { addCastTalkUser, updateCastTalkUser, showMsg } from '../../vuex/action'

export default {
  name: 'cast-talk-user-config',
  data() {
    return {
      showModal: false,
      actionType: 'ADD', // ADD or UPDATE
      form: {
        name: '',
        mobile: '',
        company: '',
        department: '',
        jobTitle: '',
        email: '',
        remark: ''
      }
    }
  },
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xAdd: addCastTalkUser,
      xUpdate: updateCastTalkUser,
      xShowMsg: showMsg
    }
  },
  methods: {
    valid() {
      let {name, mobile} = this.form
      if (!name || !mobile) {
        return Promise.reject({
          msg: '* 标识的为必填项'
        })
      }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          if (this.actionType === 'UPDATE') {
            return this.xUpdate(this.form)
          } else {
            return this.xAdd(this.form)
          }
        })
        .then(() => {
          this.showModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    }
  }
}
